﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using UCenter.PluginFramework.Utilities;

namespace UCenter.PluginFramework
{
    public class CatalogPlugInHost : PlugInHostBase
    {
        public override void Load()
        {
            OnPreLoad(new EventArgs());
            DirectoryInfo root = FilesUtils.GetRootDirectory();
            foreach (DirectoryInfo childFolder in root.GetDirectories())
            {
                FileInfo file = new FileInfo(Path.Combine(childFolder.FullName, "Manifest.xml"));
                if (file.Exists)
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ManagedPlugin));
                    FileStream fs = file.Open(FileMode.Open);
                    XmlReader reader = XmlReader.Create(fs);

                    ManagedPlugin plugin = serializer.Deserialize(reader) as ManagedPlugin;

                    foreach (AssemblyPart part in plugin.DeploymentParts)
                    {
                        string a = String.Format("{0}/{1}", plugin.Key, part.Source);

                        Assembly assembly = Assembly.LoadFrom(Path.Combine(FilesUtils.RootPath,
                            String.Format("{0}/{1}", plugin.Key, part.Source)));

                        Type[] types = assembly.GetTypes();

                        foreach (Type type in types)
                        {
                            ExtensionAttribute attribute = Attribute.GetCustomAttribute(type,
                                typeof(ExtensionAttribute)) as ExtensionAttribute;

                            if (attribute != null)
                            {
                                try
                                {
                                    IPlugInContext pluginContext = assembly.CreateInstance(type.FullName) as IPlugInContext;
                                    if (plugin.Enabled)
                                    {
                                        pluginContext.Initialize();
                                    }
                                    pluginContext.Current = plugin;

                                    if (this.Extensions.ContainsKey(plugin.Key))
                                    {
                                        this.Extensions.Remove(plugin.Key);
                                    }

                                    this.Extensions.Add(plugin.Key, pluginContext);
                                    OnLoadComplete(new EventArgs());
                                }
                                catch
                                {
                                    OnLoadFailed(new EventArgs());
                                }
                            }
                        }
                    }
                }
            }
        }

        public override void UnLoad()
        {
            OnPreUnLoad(new EventArgs());

            foreach (var item in Extensions)
            {
                if (item.Value.Current.Enabled)
                {
                    item.Value.Dispose();
                }
            }

            OnUnLoadComplete(new EventArgs());
        }
    }
}
